package com.example.lean;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;

import java.util.Random;

public class MainActivity extends AppCompatActivity implements View.OnTouchListener,View.OnClickListener,View.OnLongClickListener{

    //这里有个小技巧：在onCreate()方法的外面输入logt，然后按下Tab键，这时就会以当前的类名作为值产生一个tag常量
    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn = findViewById(R.id.button);
        btn.setOnTouchListener(this);//登录触控监听对象
        btn.setOnClickListener(this);
        btn.setOnLongClickListener(this);

        initView();
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                Log.d(TAG,"ACTION_DOWN");
                break;
            case MotionEvent.ACTION_MOVE:
                Log.d(TAG,"ACTION_MOVE");
                break;
            case MotionEvent.ACTION_UP:
                Log.d(TAG,"ACTION_UP");
                break;
        }
        return false;
    }
    @Override
    public void onClick(View v) {
        Log.d(TAG,"onClick");
    }

    @Override
    public boolean onLongClick(View v) {
        Log.d(TAG,"onLongClick");
        return false;
    }

    private TextView tvTitle;
    //按下和移动时的时间，用于判断是否是长按事件
    long timeDown, timeMove;
    //是否是长按事件
    boolean isLongClick;
    //移动相关
    float downX, downY, moveX, moveY;

    @SuppressLint("ClickableViewAccessibility")
    private void initView() {
        tvTitle = (TextView) findViewById(R.id.textView);
        tvTitle.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        Log.d(TAG, "onTouch: ACTION_DOWN");
                        timeDown = System.currentTimeMillis();
                        isLongClick = false;
                        downX = event.getRawX();
                        downY = event.getRawY();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        Log.d(TAG, "onTouch: ACTION_MOVE");
                        timeMove = System.currentTimeMillis();
                        long durationMs = timeMove - timeDown;
                        //Log.d(TAG, "onTouch: durationMs="+durationMs);
                        if (durationMs > 500) {
                            vibrate();
                            isLongClick = true;
                            Log.d(TAG, "onTouch: isLongClick=" + isLongClick);
                            //android 如何拖动控件的实现
                            //https://blog.csdn.net/bzlj2912009596/article/details/80059105
                            //长按事件，可以移动
                            moveX = event.getRawX();
                            moveY = event.getRawY();
                            /*
                            //移动的距离
                            float dx = moveX - downX;
                            float dy = moveY - downY;
                            //重新设置控件的位置。移动
                            ViewGroup.MarginLayoutParams params = (ConstraintLayout.MarginLayoutParams) tvTitle.getLayoutParams();
                            params.leftMargin += dx;
                            params.topMargin += dy;
                            tvTitle.setLayoutParams(params);
                             */
                            //移动的距离
                            int dx = (int)(moveX - downX);
                            int dy = (int)(moveY - downY);
                             // 得到imageView最开始的各顶点的坐标
                            int l = tvTitle.getLeft();
                            int r = tvTitle.getRight();
                            int t = tvTitle.getTop();
                            int b = tvTitle.getBottom();
                            // 更改imageView在窗体的位置
                            tvTitle.layout(l + dx, t + dy, r + dx, b + dy);
                            //重置
                            downX = moveX;
                            downY = moveY;
                        }
                        break;
                    case MotionEvent.ACTION_UP:
                        Log.d(TAG, "onTouch: ACTION_UP");
                        if (!isLongClick)
                            tvTitle.setText(getRandomString(10));
                        break;
                }
                return true;
            }
        });
    }

    /**
     * 震动
     */
    private void vibrate() {
        if (!isLongClick) {
            Vibrator vibrator = (Vibrator) this.getSystemService(this.VIBRATOR_SERVICE);
            vibrator.vibrate(100);
        }
    }

    /**
     * 获取一条随机字符串
     *
     * @param length
     * @return
     */
    public String getRandomString(int length) { //length表示生成字符串的长度
        String base = "abcdefghijklmnopqrstuvwxyz0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        Log.e(TAG, "getRandomString: " + sb.toString());
        return sb.toString();
    }

}